/*
This codefile contains the functionality of the "HardwareIn" 
component, specified in the design report ("tussenrapport.doc").

Author:			Daan
Version: 		1
Last modified:	09-06-11
*/

// interface/facade implementation
void HardwareIn_MoveUp (BOOL slowly)
{
	Cabin_Move(UP, slowly);
}

void HardwareIn_MoveDown (BOOL slowly)
{
	Cabin_Move(DOWN, slowly);
}

void HardwareIn_StopMoving ()
{
	Cabin_Stop();
}

void HardwareIn_SetLed (int floor)
{
	SetFloorLed(floor, ON);
}

void HardwareIn_UnsetLed (int floor)
{
	SetFloorLed(floor, OFF);
}

void HardwareIn_SetNumberDisplay (int floor)
{
	SetLedDisplay(floor);
}

void HardwareIn_SetBuzzer ()
{
	SetBuzzer(ON);
}

void HardwareIn_UnsetBuzzer ()
{
	SetBuzzer(OFF);
}



// initializing function for this component
void HardwareIn_Initialize ()
{
	// initialize IO ports
	DDR0 = 0x00; // 00000000 (0 = input, 1 = output)
	DDR1 |= 0x03; //xxxxxx1x without the OR operator, the cabin buttons do not function.
	DDR2 = 0x00; // 00000000
	DDR3 = 0xC7; // 11000111
	DDR4 = 0x07; // 00000111
	DDR5 = 0x00; // 00000000
	DDR6 = 0x08; // 00001000
	ADER = 0x00; // AD converter ports (port 5) to 'general purpose mode'
	
	// default values of the used pins
	PDR1_P11 = OFF; // Alarm buzzer
	PDR3_P30 = OFF; // Actuator clockwise (cabin downwards)
	PDR3_P31 = OFF; // Actuator anti clockwise (cabin upwards)
	PDR3_P32 = OFF; // Actuator at half speed
	PDR3_P36 = OFF; // Least significant bit of the LED display
	PDR3_P37 = OFF; // Most significant bit of the LED display
	PDR6_P63 = OFF; // LED indicator on floor 3
	PDR4_P40 = OFF; // LED indicator on floor 2
	PDR4_P41 = OFF; // LED indicator on floor 1
	PDR4_P42 = OFF; // LED indicator on floor 0
}

/*
Use the actuators to move the cabin.
	direction		boolean value for up or down.
	slowly			boolean value for moving slowly or at normal speed.
*/
void Cabin_Move (BOOL direction, BOOL slowly)
{
	if (direction == UP)
	{
		PDR3_P30 = OFF;
		PDR3_P31 = ON;
	}
	else if (direction == DOWN)
	{
		PDR3_P30 = ON;
		PDR3_P31 = OFF;
	}
	
	if (slowly == TRUE)
	{
		PDR3_P32 = ON;
	}
	else if (slowly == FALSE)
	{
		PDR3_P32 = OFF;
	}
}

/*
Turn off the actuators and thus stop the cabin.
*/
void Cabin_Stop ()
{
	PDR3_P30 = OFF;
	PDR3_P31 = OFF;
	PDR3_P32 = OFF;
}

/*
Change the LED indicator state on a floor.
	floor			floor indicator between 0 (ground floor) and 3 (upper floor).
	mode			boolean value for turning the LED on or off.
*/
void SetFloorLed (int floor, BOOL mode)
{
	if (floor == 0)
	{
		PDR4_P42 = mode;
	}
	else if (floor == 1)
	{
		PDR4_P41 = mode;
	}
	else if (floor == 2)
	{
		PDR4_P40 = mode;
	}
	else if (floor == 3)
	{
		PDR6_P63 = mode;
	}
}

/*
Change the number on the LED display.
	floor			numeric value between 0 and 3 for the corresponding floor number.
*/
void SetLedDisplay (int floor)
{
	if (floor == 0)
	{
		PDR3_P36 = OFF;
		PDR3_P37 = OFF;
	}
	else if (floor == 1)
	{
		PDR3_P36 = ON;
		PDR3_P37 = OFF;
	}
	else if (floor == 2)
	{
		PDR3_P36 = OFF;
		PDR3_P37 = ON;
	}
	else if (floor == 3)
	{
		PDR3_P36 = ON;
		PDR3_P37 = ON;
	}
}

/*
Turn the buzzer on or off.
	mode			boolean value for turning the buzzer on (1) or off (0).
*/
void SetBuzzer (BOOL mode)
{
	PDR1_P11 = mode;
}

